<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="用户信息">
                    <el-input class="w-[280px]" v-model="queryParams.keyword" placeholder="用户编号/昵称/手机号码" clearable
                        @keyup.enter="resetPage" />
                </el-form-item>
                <el-form-item label="注册时间">
                    <daterange-picker v-model:startTime="queryParams.create_time_start"
                        v-model:endTime="queryParams.create_time_end" />
                </el-form-item>
                <el-form-item label="过期时间">
                    <daterange-picker v-model:startTime="queryParams.end_time_start"
                        v-model:endTime="queryParams.end_time_end" />
                </el-form-item>
                <el-form-item label="注册来源">
                    <el-select class="w-[280px]" v-model="queryParams.channel">
                        <el-option v-for="(item, key) in ClientMap" :key="key" :label="item" :value="key" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <export-data class="ml-2.5" :fetch-fun="getUserList" :params="queryParams"
                        :page-size="pager.size" />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button class="mb-4" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <!-- <el-table-column label="用户编号" prop="sn" min-width="120" /> -->
                <el-table-column label="平台id" prop="id" min-width="70" />
                <el-table-column label="头像/昵称/账号" min-width="180">
                    <template #default="{ row }">
                        <div style="display: flex;">
                            <el-avatar :src="row.avatar" :size="50" />
                            <span style="display: flex; flex-direction: column; margin-left: 20px;">
                                <span style="font-size: 14px;font-weight: 400;">{{ row.nickname }}</span>
                                <span style="font-size: 14qb;font-weight: 400;">{{ row.account }}</span>
                            </span>
                        </div>
                    </template>
                </el-table-column>



                <el-table-column label="注册时间" prop="create_time" min-width="150" />
                <el-table-column label="过期时间" prop="end_time" min-width="100">
                    <template #default="{ row }">
                        {{ row.end_time ? row.end_time : "永久" }}
                    </template>
                </el-table-column>
                <el-table-column label="启用状态" prop="end_time" min-width="100">
                    <template #default="{ row }">
                        {{ row.is_webset == 1 ? '开启' : "关闭" }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="450" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['user.user/detail']" type="primary" link>
                            <router-link :to="{
                                path: getRoutePath('user.user/detail'),
                                query: {
                                    id: row.id,
                                },
                            }">
                                详情
                            </router-link>
                        </el-button>
                        <el-button type="primary" link @click="logon(row.id, row.login_url)">
                            登陆
                        </el-button>

                        <el-button type="primary" link @click="deleteHandle(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
        <appequity-popup v-if="showAppqueity" ref="appEquityRef" @success="getLists" @close="showAppqueity = false" />


    </div>
</template>
<script lang="ts" setup name="consumerLists">
import { usePaging } from "@/hooks/usePaging";
import { getRoutePath } from "@/router";
import { getUserList, userdel, recharge, user_oem_edit, user_oem_detail, getUserToken } from "@/api/consumer";
import { getAppEquityAppList } from "@/api/cloudupdate";
import { ClientMap } from "@/enums/appEnums";
import EditPopup from "./edit.vue";
import AppequityPopup from "./appequity.vue";
import feedback from '@/utils/feedback'
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const appEquityRef = shallowRef<InstanceType<typeof AppequityPopup>>();

const showEdit = ref(false);
const showAppqueity = ref(false);
const diamm = ref(false)
const mm = ref({
    num: "",
    id: "",
    field: "ym_num"
})
const show = ref(false)
const formshow = ref({
    copyright_text: "",
    copyright_mobile: "",
    copyright_style: 1,
    copyright_image: "",
    copyright_is_strortext: 1,
    is_copyright: 1,
    is_domain: 1,
    domain: "",
    domain_status: 1,
    is_logo: 1,
    logo: "",
    is_title: 1,
    title: "",
    is_bottom_copyright: 1,
    bottom_copyright: [
        {
            key: '',
            value: 'https://beian.miit.gov.cn/'
        }
    ],
    user_id: 0
})
const setformshow = async () => {
    console.log(formshow.value)
    await user_oem_edit(formshow.value)
    show.value = false
}
const opendomain = () => {
    window.open("https://" + formshow.value.domain + "/tenant")
}
const changestro = () => {
    formshow.value.copyright_text = ""
}
const changestyle = (type: any) => {
    formshow.value.copyright_style = type
    formshow.value.copyright_text = ""
}
const openshow = async (id: any) => {
    show.value = true
    const data = await user_oem_detail({ user_id: id })
    if (data.bottom_copyright.length == 0) {
        data.bottom_copyright = [
            {
                key: '',
                value: 'https://beian.miit.gov.cn/'
            }
        ]
    }
    console.log(data)
    formshow.value = data
    formshow.value.user_id = id
}
const updatemm = async () => {
    await recharge(mm.value)
    diamm.value = false
    getLists()
    mm.value = {
        num: "",
        id: "",
        field: "ym_num"
    }
}
const czing = (id: any) => {
    mm.value.id = id
    diamm.value = true
}
const queryParams = reactive({
    keyword: "",
    channel: "",
    create_time_start: "",
    create_time_end: "",
    end_time_start: "",
    end_time_end: "",
});

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getUserList,
    params: queryParams,
});
const payedAppList = ref<any[]>([]);


const handleAdd = async () => {
    showEdit.value = true;
    await nextTick();
    editRef.value?.open("add");
};
const handleApp = async (row: any) => {
    showAppqueity.value = true;
    await nextTick();
    appEquityRef.value?.open();
    appEquityRef.value?.setFormData(row, payedAppList.value);
};
const deleteHandle = async (id: any) => {
    await feedback.confirm('SaaS用户所有数据都将被删除,无法恢复,确定要删除吗？')
    await userdel({ id })
    getLists()
}
onActivated(() => {
    // getLists();
});
const logon = async (id: any, url: any) => {
    // window.open(id)

    //获取token
    var token = await getUserToken({ id })
    var open_url = url + '&swbs9=' + token.token

    window.open(open_url);


}
getLists();

</script>
<style lang="scss" scoped>
.boxcoprigth {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: white;
    width: 200px;
    height: 64px;
    cursor: pointer;
    margin-left: 10px;
}

.boxcopyright:first-of-type {
    margin-left: 0;
}

.border-box {
    border: 1px solid rgb(45, 140, 240)
}

.boxcopyright:first-of-type {
    margin-left: 0;
}
</style>
